<?php

namespace App\Libs\Company\SheetTemplateHandler\Aggregate;

use App\Libs\Company\SheetTemplateHandler\Aggregate\AbstractTemplate;
use App\Libs\Company\SheetTemplateHandler\Manager;
use App\Models\AggregateReport;
use App\Models\SheetTemplate;
use App\Models\Task;
use Illuminate\Support\Facades\Log;

class BaseEmployment extends AbstractTemplate
{
    public $dataStartRow = 2;

    public function generateAbstract()
    {
        $this->renderHeader();
        $this->output['sheets']['sheet-01']['cellData']['0']['0'] = $this->createTitleCell("日喀则市{$this->task->year}年{$this->task->key}月\r{$this->template->name}");

        $reportTemplates = $this->template->reportTemplates()->wherePivot('frequency', $this->task->frequency)->get();
        foreach ($reportTemplates as $rt) {
            $task = Task::where([
                'sheet_template_id' => $rt->id,
                'frequency' => $this->task->frequency,
                'year' => $this->task->year,
                'key' => $this->task->key,
                'status' => Task::STATUS_COMPLETED,
            ])
                ->first();

            $currRow = $this->dataStartRow;
            $sn = 1;
            foreach ($task->subTasks as $subTask) {
                $handler = Manager::createReportHandler(SheetTemplate::find($task->sheet_template_id), $subTask);
                $data = $handler->getData();
                $count = 0;
                try {
                    foreach ($data as $row) {
                        if (empty($row[1]['v'])) {
                            continue;
                        }
                        $this->output['sheets']['sheet-01']['cellData'][] = [
                            ['v' => $sn++, 's' => 'CENTER'],
                            ['v' => str_replace('旅游局', '', $subTask->district->name), 's' => 'CENTER'],
                            ['v' => $row[1]['v'], 't' => 1],
                            ['v' => $row[2]['v'], 't' => 2],
                            ['v' => $row[3]['v'], 't' => 2],
                            ['v' => $row[4]['v'], 't' => 2],
                        ];
                        $count++;
                    }
                } catch (\Throwable $th) {
                    Log::error('填报表异常 id:' . $subTask->id);
                    throw $th;
                }

                $this->dataEndRow = $currRow + $count - 1;
                $this->output['sheets']['sheet-01']['mergeData'][] = [
                    'startRow' => $currRow,
                    'endRow' => $this->dataEndRow,
                    'startColumn' => 1,
                    'endColumn' => 1,
                ];
                $currRow = $this->dataEndRow + 1;
            }
        }

        $this->renderFooter();
        return $this->output;
    }

    public function getData()
    {
        $aggregateReport = AggregateReport::where([
            'sheet_template_id' => $this->template->id,
            'frequency' => $this->task->frequency,
            'year' => $this->task->year,
            'key' => $this->task->key,
        ])
            ->first();
        $data = [];
        $this->dataEndRow = $this->dataStartRow;
        while (1) {
            $row = $aggregateReport->data['sheets']['sheet-01']['cellData'][$this->dataEndRow++] ?? [];
            if (!isset($row[0]['v']) || !is_numeric($row[0]['v'])) {
                break;
            }
            $data[] = $row;
        }
        $this->dataEndRow--;
        return $data;
    }
}
